package com.ctrl.riveradministrator.base;

import static com.ctrl.riveradministrator.common.Constants.BASE_URL;
import static com.ctrl.riveradministrator.common.Constants.BASE_URL_;

import com.ctrl.riveradministrator.http.APIService;
import com.ctrl.riveradministrator.http.RetrofitUtils;

import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class BasePresenter<V extends BaseView> {

    private CompositeDisposable compositeDisposable;
    private V baseView;
    private String url = BASE_URL;
    protected APIService apiService = RetrofitUtils.getInstance().getApiService();
    protected APIService apiServiceURL = RetrofitUtils.getInstance(url).getApiService();

    public BasePresenter(V baseView) {
        this.baseView = baseView;
    }

    protected void attachView(Observable<?> observable, BaseObserver observer) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(observer));
    }

    protected void attachViewSynchron(Observable<?> observable, BaseObserver observer){
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(observable.subscribeWith(observer));
    }

    protected void detachView() {
        baseView = null;
        if (compositeDisposable != null) {
            compositeDisposable.dispose();
        }
    }

    protected V getBaseView(){
        return baseView;
    }
}
